package com.ruoyi.zsyh.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.zsyh.domain.ZsyhAgentInfo;
import com.ruoyi.zsyh.domain.ZsyhAgentOrder;
import com.ruoyi.zsyh.domain.ZsyhAgentOrderRel;
import com.ruoyi.zsyh.domain.ZsyhAgentReport;
import com.ruoyi.zsyh.service.IZsyhAgentOrderRelService;
import com.ruoyi.zsyh.service.IZsyhAgentOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 
 * @author ruoyi
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/zsyh/report")
public class ZsyhReportController extends BaseController
{
    private String prefix = "zsyh/report";

    @Autowired
    private IZsyhAgentOrderRelService zsyhAgentOrderRelService;

    @RequiresPermissions("zsyh:report:view")
    @GetMapping("/agentReport")
    public String report()
    {
        return prefix + "/agentReport";
    }

    @RequiresPermissions("zsyh:report:view")
    @GetMapping("/mainReport")
    public String mainReport()
    {
        return prefix + "/mainReport";
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("zsyh:report:list")
    @PostMapping("/listAgentReport")
    @ResponseBody
    public TableDataInfo list(String yearSearch, String monthSearch) throws Exception {
        startPage();
        ZsyhAgentOrderRel zsyhAgentOrderRel = new ZsyhAgentOrderRel();
        if(EmptyUtils.isNotEmpty(yearSearch) && EmptyUtils.isNotEmpty(monthSearch)){
            zsyhAgentOrderRel.setPeriodYear(Integer.parseInt(yearSearch));
            zsyhAgentOrderRel.setPeriodMonth(Integer.parseInt(monthSearch));
        }else{
            Calendar calendar = Calendar.getInstance();
            zsyhAgentOrderRel.setPeriodYear(calendar.get(Calendar.YEAR));
            zsyhAgentOrderRel.setPeriodMonth(calendar.get(Calendar.MONTH) + 1);
        }
        List<ZsyhAgentOrderRel> list = zsyhAgentOrderRelService.getAgentOrderRelMonthStatistics(zsyhAgentOrderRel);
        return getDataTable(list);
    }

    /**
     * 导出大区会长预订单列表
     */
    @RequiresPermissions("zsyh:report:export")
    @Log(title = "大区会长预订单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAgent")
    @ResponseBody
    public AjaxResult exportAgent(String yearSearch, String monthSearch)
    {
        ZsyhAgentOrderRel zsyhAgentOrderRel = new ZsyhAgentOrderRel();
        if(EmptyUtils.isNotEmpty(yearSearch) && EmptyUtils.isNotEmpty(monthSearch)){
            zsyhAgentOrderRel.setPeriodYear(Integer.parseInt(yearSearch));
            zsyhAgentOrderRel.setPeriodMonth(Integer.parseInt(monthSearch));
        }else{
            Calendar calendar = Calendar.getInstance();
            zsyhAgentOrderRel.setPeriodYear(calendar.get(Calendar.YEAR));
            zsyhAgentOrderRel.setPeriodMonth(calendar.get(Calendar.MONTH) + 1);
        }
        List<ZsyhAgentOrderRel> list = zsyhAgentOrderRelService.selectZsyhAgentOrderRelCurrentMonth(zsyhAgentOrderRel);
        ExcelUtil<ZsyhAgentOrderRel> util = new ExcelUtil<ZsyhAgentOrderRel>(ZsyhAgentOrderRel.class);
        return util.exportExcel(list, "大区会长预订单数据");
    }
}
